package com.ruoyi.project.WebMoudle.WxShopType.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.project.WebMoudle.WxShop.domain.ShopEntity;
import com.ruoyi.project.WebMoudle.WxShop.service.IWxShopService;
import com.ruoyi.project.WebMoudle.shopCarouse.domain.ShopCarousel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.WxShopType.domain.WxShopType;
import com.ruoyi.project.WebMoudle.WxShopType.service.IWxShopTypeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商铺类型Controller
 * 
 * @author ruoyi
 * @date 2020-06-18
 */
@Controller
@RequestMapping("/WxShopType/WxShopType")
public class WxShopTypeController extends BaseController
{
    private String prefix = "WxShopType/WxShopType";

    @Autowired
    private IWxShopTypeService wxShopTypeService;

    @Autowired
    private IWxShopService wxShopService;

    @RequiresPermissions("WxShopType:WxShopType:view")
    @GetMapping()
    public String WxShopType()
    {
        return prefix + "/WxShopType";
    }

    /**
     * 查询商铺类型列表
     */
    @RequiresPermissions("WxShopType:WxShopType:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxShopType wxShopType)
    {
        startPage();
        List<WxShopType> list = wxShopTypeService.selectWxShopTypeList(wxShopType);
        return getDataTable(list);
    }

    /**
     * 导出商铺类型列表
     */
    @RequiresPermissions("WxShopType:WxShopType:export")
    @Log(title = "商铺类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxShopType wxShopType)
    {
        List<WxShopType> list = wxShopTypeService.selectWxShopTypeList(wxShopType);
        ExcelUtil<WxShopType> util = new ExcelUtil<WxShopType>(WxShopType.class);
        return util.exportExcel(list, "WxShopType");
    }

    /**
     * 新增商铺类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商铺类型
     */
    @RequiresPermissions("WxShopType:WxShopType:add")
    @Log(title = "商铺类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("imgFile") MultipartFile imgFile, WxShopType wxShopType)
    {
        try {
            //上传图片
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String imgName = FileUploadUtils.upload(filePath, imgFile);
            wxShopType.setImgUrl(imgName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return toAjax(wxShopTypeService.insertWxShopType(wxShopType));
    }

    /**
     * 修改商铺类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WxShopType wxShopType = wxShopTypeService.selectWxShopTypeById(id);
        mmap.put("wxShopType", wxShopType);
        return prefix + "/edit";
    }

    /**
     * 修改保存商铺类型
     */
    @RequiresPermissions("WxShopType:WxShopType:edit")
    @Log(title = "商铺类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile imgFile,WxShopType wxShopType) throws IOException {
        if (imgFile != null) {
            String filePath = RuoYiConfig.getUploadPath();
            String fileUrl = FileUploadUtils.upload(filePath, imgFile);
            wxShopType.setImgUrl(fileUrl);
        }
        return toAjax(wxShopTypeService.updateWxShopType(wxShopType));
    }

    /**
     * 删除商铺类型
     */
    @RequiresPermissions("WxShopType:WxShopType:remove")
    @Log(title = "商铺类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //查询该类别下是否存在商铺还\
        List<ShopEntity> wxShopList=wxShopService.selectShopsByType(ids);
        if (wxShopList.size()>0){
            return AjaxResult.warn("该类别存在商铺,不允许删除");
        }
        return toAjax(wxShopTypeService.deleteWxShopTypeById(Long.parseLong(ids)));
    }

    /**
     * 校验类别名称唯一
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkNameUnique(WxShopType shopType)
    {
        return wxShopTypeService.checkNameUnique(shopType);
    }


    /**
     * 校验序号唯一
     */
    @PostMapping("/checkSortNumUnique")
    @ResponseBody
    public String checkSortNumUnique(WxShopType shopType)
    {
        return wxShopTypeService.checkSortNumUnique(shopType);
    }

    @RequiresPermissions("WxShopType:WxShopType:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(WxShopType shopType)
    {
        return toAjax(wxShopTypeService.updateWxShopType(shopType));
    }

}
